#include "my_gui.h"
#include "lvgl.h"

LV_IMG_DECLARE(img_cool_on);
LV_IMG_DECLARE(img_cool_off);

void gui_textarea(void)
{
    lv_obj_t *textarea = lv_textarea_create(lv_scr_act());
    lv_textarea_add_char(textarea, 'A');
    lv_textarea_add_text(textarea, "BCDEFG");
    lv_obj_t *keyboard = lv_keyboard_create(lv_scr_act());
    lv_keyboard_set_textarea(keyboard, textarea);
    lv_textarea_set_cursor_pos(textarea, 1);
    lv_textarea_del_char(textarea);
    lv_textarea_del_char_forward(textarea);
    lv_textarea_set_one_line(textarea, true);
    lv_textarea_set_password_mode(textarea, true);  //密码模式
    lv_textarea_set_password_show_time(textarea, 100);
    lv_textarea_set_accepted_chars(textarea, "12345678");  //限制字符输入
    lv_textarea_set_max_length(textarea, 10);   //限制字符输入长度
}

static void gui_textarea1_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    char *text = lv_textarea_get_text(target);
    printf("text: %s\n", text);
    if (!strcmp(text, "12345678")) {
        printf("text match\n");
    } else {
        printf("text not match\n");
    }
}
void gui_textarea1(void)
{
    lv_obj_t *textarea = lv_textarea_create(lv_scr_act());
    lv_textarea_set_placeholder_text(textarea, "password");
    lv_obj_t *keyboard = lv_keyboard_create(lv_scr_act());
    lv_keyboard_set_textarea(keyboard, textarea);
    lv_obj_add_event_cb(textarea, gui_textarea1_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

void gui_keyboard(void)
{
    lv_obj_t *keyboard = lv_keyboard_create(lv_scr_act());
    lv_obj_t *textarea = lv_textarea_create(lv_scr_act());
    lv_keyboard_set_textarea(keyboard, textarea);
    lv_keyboard_set_popovers(keyboard, true); //设置按键弹窗
    lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_NUMBER); //数字键盘模式
}

static void gui_imgbtn_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_obj_set_style_img_recolor(target, lv_color_hex(0xff0000), LV_STATE_DEFAULT);
    lv_obj_set_style_img_recolor_opa(target, 150, LV_STATE_DEFAULT);
}
void gui_imgbtn(void)
{
    lv_obj_t *imgbtn = lv_imgbtn_create(lv_scr_act());
    lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_RELEASED, NULL, &img_cool_off, NULL);
    lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_PRESSED, NULL, &img_cool_on, NULL);
    lv_obj_set_size(imgbtn, 64, 64);
    //lv_imgbtn_set_state(imgbtn, LV_IMGBTN_STATE_PRESSED);
    lv_obj_add_event_cb(imgbtn, gui_imgbtn_cb, LV_EVENT_PRESSED, NULL);
}

void gui_tabview(void)
{
    lv_obj_t *tabview = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, 50);
    lv_obj_t *tab0 = lv_tabview_add_tab(tabview, "Tab1");
    lv_obj_t *tab1 = lv_tabview_add_tab(tabview, "Tab2");
    lv_obj_t *tab2 = lv_tabview_add_tab(tabview, "Tab3");
    lv_obj_t *label = lv_label_create(tab0);
    lv_tabview_set_act(tabview, 1, LV_ANIM_OFF);
}

void gui_tileview(void)
{
    lv_obj_t *tileview = lv_tileview_create(lv_scr_act());
    lv_obj_t *tile0 = lv_tileview_add_tile(tileview, 0, 0, LV_DIR_RIGHT);
    lv_obj_t *tile1 = lv_tileview_add_tile(tileview, 1, 0, LV_DIR_LEFT);
    lv_obj_t *label = lv_label_create(tile0);
    lv_obj_update_layout(tileview);
    //lv_obj_set_tile(tileview, tile1, LV_ANIM_OFF);
    lv_obj_set_tile_id(tileview, 1, 0, LV_ANIM_OFF);
}

void gui_win(void)
{
    lv_obj_t *win = lv_win_create(lv_scr_act(), 40);
    lv_obj_set_size(win, 300, 200);
    lv_obj_center(win);
    lv_obj_set_style_border_width(win, 1, LV_STATE_DEFAULT);
    lv_obj_t *title = lv_win_add_title(win, "Settings");
    lv_obj_t *btn = lv_win_add_btn(win, LV_SYMBOL_CLOSE, 20);
    lv_obj_t *content = lv_win_get_content(win); //获取主体
    lv_obj_t *label = lv_label_create(content);
}

static void gui_msgbox_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_current_target(e);
    printf("%d \n", lv_msgbox_get_active_btn(target));
    printf("%s \n", lv_msgbox_get_active_btn_text(target));
}
void gui_msgbox(void)
{
    static const char *btns[] = {"Continue", "Close",""};
    lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(), "Notice", "Do you want to continue?", btns, true);
    lv_obj_center(msgbox);
    //lv_msgbox_close(msgbox);
    lv_obj_add_event_cb(msgbox, gui_msgbox_cb, LV_EVENT_VALUE_CHANGED, NULL);
}

static lv_obj_t *spinbox;
static void gui_btn1_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_spinbox_increment(spinbox);
    printf("%d \n", lv_spinbox_get_value(spinbox));
}
static void gui_btn2_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_spinbox_decrement(spinbox);
    printf("%d \n", lv_spinbox_get_value(spinbox));
}
void gui_spinbox(void)
{
    spinbox = lv_spinbox_create(lv_scr_act());
    lv_obj_center(spinbox);
    lv_spinbox_set_step(spinbox, 2);
    lv_spinbox_set_range(spinbox, -20, 20);
    lv_spinbox_set_value(spinbox, 18);
    lv_spinbox_set_digit_format(spinbox, 6, 2); //设置数字位数，小数点位置
    //lv_spinbox_set_pos(spinbox, 2);
    lv_obj_t *btn1 = lv_btn_create(lv_scr_act());
    lv_obj_t *label = lv_label_create(btn1);
    lv_label_set_text(label, "+");
    lv_obj_align_to(btn1, spinbox, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(btn1, gui_btn1_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *btn2 = lv_btn_create(lv_scr_act());
    lv_obj_t *labe2 = lv_label_create(btn2);
    lv_label_set_text(labe2, "-");
    lv_obj_align_to(btn2, spinbox, LV_ALIGN_OUT_LEFT_MID, -10, 0);
    lv_obj_add_event_cb(btn2, gui_btn2_cb, LV_EVENT_CLICKED, NULL);
}

void gui_table(void)
{
    lv_obj_t *table = lv_table_create(lv_scr_act());
    lv_obj_center(table);
    lv_table_set_row_cnt(table, 2);
    lv_table_set_col_cnt(table, 3);
    lv_table_set_col_width(table, 0, 200);
    lv_table_set_cell_value(table, 0, 0, "123");
    lv_table_set_cell_value_fmt(table, 1, 0, "%d",66);
}

void gui_group(void)
{
    //lv_group_t *g = lv_group_create(lv_scr_act());
    //lv_group_set_default(g);
    //lv_group_add_obj(g, obj);
    //lv_indev_set_group(indev_keypad, g);
}

LV_FONT_DECLARE(Font30)
void gui_font(void)
{
    lv_obj_t *font_label = lv_label_create(lv_scr_act());
    lv_obj_set_style_text_font(font_label, &Font30, LV_STATE_DEFAULT);
    lv_label_set_text(font_label, "你好，正点原子!666666");
    lv_obj_center(font_label);
}

LV_FONT_DECLARE(ganggang)
void gui_font1(void)
{
    lv_obj_t *font_label = lv_label_create(lv_scr_act());
    lv_obj_set_style_text_font(font_label, &ganggang, LV_STATE_DEFAULT);
    lv_label_set_text(font_label, "罡罡冰壶");
    lv_obj_center(font_label);
}

lv_obj_t *speed;
static void gui_btn11_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_spinbox_increment(spinbox);
    lv_label_set_text_fmt(speed, "speed:%d", lv_spinbox_get_value(spinbox));
    printf("%d \n", lv_spinbox_get_value(spinbox));
}
static void gui_btn22_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_spinbox_decrement(spinbox);
    lv_label_set_text_fmt(speed, "speed:%d", lv_spinbox_get_value(spinbox));
    printf("%d \n", lv_spinbox_get_value(spinbox));
}
static void gui_imgbtn1_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    printf("11111111\n");
}
static void gui_imgbtn2_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    printf("2222222\n");
}

static lv_obj_t *slider_label;
static void esc_slider_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_label_set_text_fmt(slider_label,"%d%%", lv_slider_get_value(target));
    printf("slider: %d\n", lv_slider_get_value(target));
}

void gui_spinbox2(void)
{
    lv_obj_t *tv = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, 50);
    lv_obj_t *tvtab = lv_tabview_add_tab(tv, "BLDC");

    spinbox = lv_spinbox_create(tvtab);
    lv_obj_set_size(spinbox, 130, 40);
    lv_obj_align_to(spinbox, tvtab, LV_ALIGN_TOP_MID, 0, 10);
    lv_spinbox_set_digit_format(spinbox, 5, 0); //设置数字位数，小数点位置
    lv_spinbox_set_step(spinbox, 500);
    lv_spinbox_set_range(spinbox, -10000, 10000);
    lv_spinbox_set_value(spinbox, 500);
    //lv_spinbox_set_pos(spinbox, 2);
    lv_obj_t *btn1 = lv_btn_create(tvtab);
    lv_obj_set_size(btn1, 40, 40);
    lv_obj_t *label = lv_label_create(btn1);
    lv_label_set_text(label, "+");
    lv_obj_center(label);
    lv_obj_align_to(btn1, spinbox, LV_ALIGN_OUT_RIGHT_MID, 10, 0);
    lv_obj_add_event_cb(btn1, gui_btn11_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_t *btn2 = lv_btn_create(tvtab);
    lv_obj_set_size(btn2, 40, 40);
    lv_obj_t *labe2 = lv_label_create(btn2);
    lv_obj_center(labe2);
    lv_label_set_text(labe2, "-");
    lv_obj_align_to(btn2, spinbox, LV_ALIGN_OUT_LEFT_MID, -10, 0);
    lv_obj_add_event_cb(btn2, gui_btn22_cb, LV_EVENT_CLICKED, NULL);

    speed = lv_label_create(tvtab);
    lv_label_set_text_fmt(speed, "speed:%d", lv_spinbox_get_value(spinbox));
    lv_obj_align_to(speed, spinbox, LV_ALIGN_OUT_BOTTOM_MID, 0, 40);

    lv_obj_t *imgbtn = lv_imgbtn_create(tvtab);
    lv_obj_align_to(imgbtn, speed, LV_ALIGN_OUT_BOTTOM_MID, 32, 40);
    lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_RELEASED, NULL, &img_cool_off, NULL);
    lv_imgbtn_set_src(imgbtn, LV_IMGBTN_STATE_PRESSED, NULL, &img_cool_on, NULL);
    lv_obj_set_size(imgbtn, 64, 64);
    lv_obj_add_event_cb(imgbtn, gui_imgbtn1_cb, LV_EVENT_PRESSED, NULL);
    lv_obj_add_event_cb(imgbtn, gui_imgbtn2_cb, LV_EVENT_RELEASED, NULL);

    lv_obj_t *dttab = lv_tabview_add_tab(tv, "ESC");
    lv_obj_t *slider = lv_slider_create(dttab);
    lv_obj_set_size(slider, 200, 20);
    lv_obj_center(slider);
    lv_slider_set_value(slider, 0, LV_ANIM_OFF);
    lv_slider_set_range(slider, 0, 100);
    lv_obj_add_event_cb(slider, esc_slider_cb, LV_EVENT_VALUE_CHANGED, NULL);
    slider_label = lv_label_create(dttab);
    lv_obj_align_to(slider_label, slider, LV_ALIGN_OUT_TOP_MID, 0, -50);
    lv_label_set_text(slider_label,"0%");

    lv_tabview_set_act(tv, 0, LV_ANIM_OFF);
}

void gui_demo(void)
{
    //gui_textarea();
    //gui_textarea1();
    //gui_keyboard();
    //gui_imgbtn();
    //gui_tabview();
    //gui_tileview();
    //gui_win();
    //gui_msgbox();
    //fgui_spinbox();
    //gui_table();
    //gui_font();
    gui_font1();
    //gui_spinbox2();
}

